//
//  SEGInviteRulesViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/3/4.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGInviteRulesViewController.h"
#import "SEGInviteRulesViewModel.h"

@interface SEGInviteRulesViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSMutableArray* rulesHeight;
@property(nonatomic, strong) SEGInviteRulesViewModel *viewModel;
@end

@implementation SEGInviteRulesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"邀请规则";
    
    self.view.backgroundColor = kColor_W;
    
    self.rulesHeight = [NSMutableArray array];
    for (int i = 0; i < self.rules.count; i++) {
        NSString *rule = [NSString stringWithFormat:@"%@",self.rules[i]];
        [self.rules replaceObjectAtIndex:i withObject:rule];
        CGSize sizeToFit = [rule sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(LYScreenWidth()-30, 10000) lineBreakMode:NSLineBreakByCharWrapping];
        [self.rulesHeight addObject:@(sizeToFit.height+13)];
    }
    
    // 列表设置
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = kColor_W;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-43);
        make.top.equalTo(self.view.mas_top);
    }];
    
    
    UIView* bottomView = [[UIView alloc] initWithFrame:CGRectZero];
    bottomView.backgroundColor = kColor_W;
    UILabel* label = [[UILabel alloc] init];
    label.text = [NSString stringWithFormat:@"%@保留对本规则的最终解释权",kAppName];
    label.textColor = [UIColor colorWithRed:182/255.0 green:182/255.0 blue:182/255.0 alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    [bottomView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bottomView);
        make.centerY.equalTo(bottomView);
    }];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_bottom).with.offset(-43);
        make.height.mas_equalTo(43);
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.rules.count;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath];
    cell.textLabel.text = self.rules[indexPath.row];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = kColor_2;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return [self.rulesHeight[indexPath.row] floatValue];
}
- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    return 46;
}

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), 46)];
    view.backgroundColor = [UIColor clearColor];
    UILabel* label = [[UILabel alloc] init];
    label.text = @"邀请规则说明";
    label.textColor = kColor_1;
    label.font = [UIFont systemFontOfSize:16];
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view).with.offset(15);
        make.centerY.equalTo(view);
    }];
    return view;
}
#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGInviteRulesViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGInviteRulesViewModel alloc]init];
    }
    return _viewModel;
}
@end
